<?php

namespace Unit;

use app\agent\controller\coupon\Url;
use app\api\controller\JdCallCharges;
use app\common\service\takeout\JtkTakeoutService;
use app\model\CouponBrand;
use app\model\CouponProductChannel;
use PHPUnit\Framework\TestCase;

class CouponGetExtendsion extends TestCase
{

    /**
     * 获取推广链接
     */
    public function testGetProductUrl(){

        /**@var CouponProductChannel $couponProductChannel**/
        $couponProductChannel = CouponProductChannel::query()->whereNotNull('product_id')->find();

        request()->setParam([
            'id' => $couponProductChannel->id,
            'scene' => 'product'
        ]);

        $app = app();

        $app->http->run(request());

        $obj = new Url($app);

        $data = $obj->generate();

        $data = json_decode($data->getContent(),true);

        $this->assertTrue($data['code'] ==200,$data['message']);

    }

    /**
     * 获取品牌推广链接
     */
    public function testGetBrandUrl(){

        /**@var CouponBrand $couponBrand**/
        $couponBrand = CouponBrand::orderByDesc()->find();


        request()->setParam([
            'id' => $couponBrand->id,
            'scene' => 'brand'
        ]);

        $app = app();

        $app->http->run(request());

        $obj = new Url($app);

        $data = $obj->generate();

        $data = json_decode($data->getContent(),true);

        $this->assertTrue($data['code'] ==200,$data['message']);
    }

    /**
     * 获取cps推广链接
     */
    public function testGetCpsUrl(){

        /**@var CouponBrand $couponBrand**/
        $couponBrand = CouponBrand::orderByDesc()->find();

        request()->setParam([
            'id' => $couponBrand->id,
            'scene' => 'cps'
        ]);

        $app = app();

        $app->http->run(request());

        $obj = new Url($app);

        $data = $obj->generate();

        $data = json_decode($data->getContent(),true);


        $this->assertTrue($data['code'] ==200,$data['message']);
    }

    /**
     * 获取cps推广链接
     */
    public function testFailUrl(){
        request()->setParam([
            'id' => '1',
            'scene' => 'product1'
        ]);

        $app = app();

        $app->http->run(request());

        $obj = new Url($app);

        $data = $obj->generate();

        $data = json_decode($data->getContent(),true);

        $this->assertTrue($data['code'] !=200,$data['message']);
    }

    /**
     * 美团券包 生成推广链接
     * @return void
     */
    public function testGetMeituanCouponUrl(){
        $params = [
            'page' => 1,
            'list_rows' => 1,
        ];
        $data = (new JtkTakeoutService())->meituanCouponAdList($params);

        $first = Collect($data['data'])->first();
        if (empty($first)) {
            $this->assertTrue(false, '美团券包数据不存在');
        }

        request()->setParam([
            'id' => $first['coupon_id'],
            'scene' => 'coupon'
        ]);

        $app = app();

        $app->http->run(request());

        $obj = new Url($app);

        $data = $obj->generate();

        $data = json_decode($data->getContent(),true);

        $this->assertTrue($data['code'] ==200,$data['message']);
    }


}